

$.blockUI.defaults.css.border = 'none';
$.blockUI.defaults.css.padding = '15px';
$.blockUI.defaults.css.backgroundColor = '#000';
$.blockUI.defaults.css.opacity = '0.6';
$.blockUI.defaults.css.color = '#fff';
//$.blockUI.defaults.css.message = '<h1><img src="./busy.gif" /> Loading...</h1>';
$.blockUI.defaults.css.timeout = '1000';

function loadDocument(typeId){
	$.blockUI();
	
	var url = getWorkFlowURL();
	
	$.post(url,{ajaxSource:'ajaxRequest',mode:'embedded',_eventId:'browseDocument',typeId:typeId},function(data,response){
		$("#searchResult").empty().html(data);
		$.unblockUI(); 
	});
}

function setPageTitle(titleText){
	document.title = titleText;
}

function loadContentFromMainMenu(urlToLoad){
	//showLoadingDialog("Loading..",5000);
	$.blockUI();
	
	$.post(contextRoot + urlToLoad,{ajaxSource:'ajaxRequest',mode:'embedded'},function(data){
		$(mainContentDiv).empty().html(data);
		//hideLoadingDialog();
		$.unblockUI(); 
	});
	
}
function createMapUserDialog()
{
	mapUserDialog = $("#mapUsersDiv").dialog({
		
		height: 500,
		width: 850,
		modal: true,
		autoOpen: true,
		dialogClass : 'mapUserDialog',
		closeOnEscape :false
	});
	var numOfDialog = $(".mapUserDialog").length;
	if(numOfDialog > 1)
		{
			$(".mapUserDialog").first().empty().remove();
		}
}
function createSerachUserDialog()
{
	   userDialog = $("#userSearchDialogue").dialog({
	   		height: 350,
	  		width: 750,
	  		modal: true,
	  		autoOpen: false
	   	});
}
function openMapUserDialog()
{
	mapUserDialog.dialog("open");
}
function createLayoutForAddOrEdit(northDiv,tabsDiv){
	addOrEditLayout = $(mainContentDiv).layout({
		center__paneSelector : "#" + tabsDiv,
		north__paneSelector: "#" + northDiv,
		north__spacing_open: 0,
		north__spacing_closed:0,
		north__size:35,
		north__closable:false,
		north__resizable:false
		
	});
	
	//$("#" + tabsDiv).tabs();
	addOrEditLayout.resizeAll();
}

function textCounter(field,maxlimit) {
	var curVal = $("#"+field).val();
	var len = curVal.length;
	if(len > maxlimit) // if too long...trim it!
		$("#"+field).val(curVal.substring(0, maxlimit));
	else
		$("#"+field+"Count").val(maxlimit - len);
}

function enableDatepicker(elementId,dateFormat){
//	$("#" + elementId).datepicker({dateFormat:dateFormat});
	$("#" + elementId).datepicker({changeMonth: true, changeYear: true, showOptions: {direction: 'up' }, dateFormat: dateFormat});
}

function enableDisableFields(selectId){
	var value = $("#" + selectId).val();
	
	if(value == "false"){
		$("input.disableInputField").each(function(index){
			$(this).attr("disabled","disabled");
			//$(this).addClass('infoTextBox highLightText1');
		});
	}else{
		$("input.disableInputField").each(function(index){
			$(this).removeAttr("disabled");
		});
	}
}

function renewDocument(docId)
{
	//showLoadingDialog("Loading..",5000);
	$.blockUI();
	
	var url = getWorkFlowURL();
	$.post(url,{ajaxSource:'ajaxRequest',mode:'embedded',_eventId:'renew',docId:docId},function(data,response){
		$(mainContentDiv).empty().html(data);
		//hideLoadingDialog();
		$.unblockUI(); 
	});
	
}
function viewDocument()
{
	//showLoadingDialog("Loading..",5000);
	$.blockUI();
	 
	var url = getWorkFlowURL();
	var docId = $("#searchDocId").val();
	if(docId==0)
		{
			alert("Please Select a Document From document Lookup");
			documentlookup();
			showDocumentLookUpDialog();
			$.unblockUI(); 
			//hideLoadingDialog();			
		}
	
	else{
		$.post(url,{ajaxSource:'ajaxRequest',mode:'embedded',_eventId:'view',docId:docId},function(data,response){
			$(mainContentDiv).empty().html(data);
			//hideLoadingDialog();
			$.unblockUI(); 
			});
	}
	
}
function moveToViewGroup(){
    $("#userList > li.ui-selected").each(function(index){
            var liId = $(this).attr('id');
            var liText = $(this).html();
            $(this).remove();
            $("#viewGroupList").append("<li id='"+ liId +"' class='ui-state-default'>"+ liText +"</li>");
            $("#viewGroupDiv").append("<input id='viewGroup"+ liId +"' type='hidden' name='viewGroup' value='"+ liId +"'>");
        });
    //$("#viewGroupList").selectable("refresh");
   
}
function moveToPrintGroup(){
    $("#userList > li.ui-selected").each(function(index){
            var liId = $(this).attr('id');
            var liText = $(this).html();
            $(this).remove();
            $("#printGroupList").append("<li id='"+ liId +"' class='ui-state-default'>"+ liText +"</li>");
            $("#printGroupDiv").append("<input id='printGroup"+ liId +"' type='hidden' name='printGroup' value='"+ liId +"'>");
        });
    //$("#viewGroupList").selectable("refresh");
   
}
function searchUser()
{
	//showLoadingDialog("Loading..",5000);
	$.blockUI();
	
	var url = getWorkFlowURL();
	var userName=$("#userDisplayName").val();
	var userId=$("#userName").val();
	var searchEvent = 'changePermission';
	$.post(url,{ajaxSource:'ajaxRequest',mode:'embedded',_eventId:'searchUser',userId:userId,userName:userName,searchEvent:searchEvent},function(data,response){
		$("#userSearchDialogueResult").empty().html(data);
		//hideLoadingDialog();
		$.unblockUI(); 
	});
}
function chooseUser(userName,userid)
{
//	var options = '<li id="'+userid+'" class="ui-state-default">'+ userName+ " (" + userid +")"+'</li>';
	
//	var count = $("tr.count").length;
//	alert(count);
	
	var row2 = '<tr class = "odd ui-selectee" id="user_' + userid + '" ><td class="ui-selectee">' + userName + "(" + userid + ")" + '</td>';
	row2 += '<td class="ui-selectee"> <input class="ui-selectee" type="checkbox" value="' + userid + '" name="viewGroup">';
	row2 += '<input class="ui-selectee" type="hidden" value="on" name="_viewGroup" style=""> </td>';
	
	row2 += '<td class="ui-selectee"> <input class="ui-selectee" type="checkbox" value="' + userid + '" name="printGroup">';
	row2 += '<input class="ui-selectee" type="hidden" value="on" name="_printGroup" style=""> </td></tr>';

//	<form:checkbox path="viewGroup" value="${userMap.key}"/>
//	<input class="ui-selectee" type="hidden" value="on" name="_viewGroup" style="">
//	<input id="viewGroup5" class="ui-selectee" type="checkbox" value="controller" name="viewGroup">
//	<input class="ui-selectee" type="hidden" value="on" name="_viewGroup" style="">
	
	$("#userListForMapping").append(row2);

}
function closeChooseUserDialogue(){
	$("#userSearchDialogue").dialog("close");
}
function moveToUserListFromPrint(){
    $("#printGroupList > li.ui-selected").each(function(index){
            var liId = $(this).attr('id');
            var liText = $(this).html(); 
            $(this).remove();
            $("#"+liId).remove();
            $("#printGroup"+liId).each(function(index){ $(this).remove(); });
            $("#userList").append("<li id='"+ liId +"' class='ui-state-default'>"+ liText +"</li>");
        });
}
function moveToUserListFromView(){
    $("#viewGroupList > li.ui-selected").each(function(index){
            var liId = $(this).attr('id');
            var liText = $(this).html();
            $(this).remove();
            $("#"+liId).remove();
            $("#viewGroup"+liId).each(function(index){ $(this).remove(); });
            $("#userList").append("<li id='"+ liId +"' class='ui-state-default'>"+ liText +"</li>");
        });
}
function saveActivityMapping(formId)
{
	//showLoadingDialog("Loading..",5000);
	$.blockUI();
	
	var url = getWorkFlowURL();
	var formData = $("#"+formId).formSerialize();
	formData = formData+"&ajaxSource=ajaxRequest&mode=embedded&_eventId=submit&remarks=remarks";
	$.post(url,formData,function(data,response){
		$("#mapUsersDiv").empty().html(data);
		$.unblockUI(); 
	});	
}

function saveUsersPermissions(formId)
{
	//showLoadingDialog("Loading..",5000);
	$.blockUI();
	
	var url = getWorkFlowURL();
	var formData = $("#"+formId).formSerialize();
	formData = formData+"&ajaxSource=ajaxRequest&mode=embedded&_eventId=submit&remarks=remarks";
	$.post(url,formData,function(data,response){
		if(response == "success")
			alert("Permissions changed successfully.");
		$("#changePermissionDiv").empty().html(data);
		$.unblockUI(); 
	});	
}

function openSearchUser()
{
	$("#userSearchDialogue").dialog("open");
}

function submitSelectedUsers(){
	
	var selectedUsers = new Array();
	$("input:checkbox[name=check]:checked").each(function(){
		selectedUsers.push($(this).val());
	});
	var alreadyExist = false;
	for(var i=0; i<selectedUsers.length; i++) {
		var value = selectedUsers[i];
		var splitvar = value.split(',');
		if($("#user_"+splitvar[1]).length == 0)
			chooseUser(splitvar[0],splitvar[1]);
		else
			alreadyExist = true;
	}
	
	if(alreadyExist){
		alert("Some of the selected users are already selected.");
	}
	$("#userSearchDialogue").dialog("close");
	$("#userSearchDialogueResult").empty().html('');
}

function viewDocumentDetail(docId,event){
	$.blockUI();
	var url = getWorkFlowURL();
	$.post(url,{ajaxSource:'ajaxRequest',_eventId:event,mode:'embedded',docId:docId},function(data,response){
		$(mainContentDiv).empty().html(data);
		$.unblockUI();
	});	
}

function closeMapUserDialog(){
	mapUserDialog.dialog("close");

}
function mapUsersToDocument(docId){
	var url = getWorkFlowURL();
	$.post(url,{ajaxSource:'ajaxRequest',_eventId:'transitionState',mode:'embedded',activity:'mapUsers',docId:docId},function(data){
		$("#mapUsersDiv").empty().html(data);
		//hideLoadingDialog();
		$.unblockUI(); 
		});	
}

function loadAdminWindow(){
	$.blockUI();
	$.post('welcomeMe.htm',{data:'data'},function(data){
		$(mainContentDiv).empty().html(data);
		$.unblockUI();
	},"html");
}

function loadHomePage(){
	$.blockUI();
	
	$.post('loadDashBoard.htm',{data:'data'},function(response,status){
		$("#contentDiv").empty().html(response);
		$.unblockUI();
	});
	
	
}

function resetForm(formId) {
	$("#"+ formId).find(':input').each(function() {
	       switch(this.type) {
	           case 'select-one':
	           	$(this).val([0]);
	               break;
	           break;
	           case 'password':
	           case 'textarea':
	           case 'text':
	           case 'file':	
	               $(this).val('');
	               break;
	           case 'radio':
	           case 'checkbox':
	               this.checked = false;
	       }
	   });
}